package com.example.administrator.printoutindemo.view;

import android.content.Context;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

public class MyImageButton extends ImageButton implements OnTouchListener,
OnFocusChangeListener {

/**
* ��ɫ���˾�����һ��4x5�ľ������зֱ��Ǻ�ɫͨ��ֵ����ɫͨ��ֵ����ɫͨ��ֵ��alphaͨ��ֵ�����зֱ��Ƕ�Ӧͨ���ĺ�ɫֵ����ɫֵ����ɫֵ��
* alphaֵ��ƫ������ RGB��Alpha����ֵ���㷽�����£�
* Redͨ����ֵ= a[0] * srcR + a[1] * srcG + a[2]* srcB + a[3] * srcA + a[4] 
* Greenͨ����ֵ= a[5] * srcR + a[6] * srcG + a[7] * srcB + a[8] * srcA + a[9] 
* Blueͨ����ֵ= a[10] * srcR + a[11] * srcG + a[12] * srcB + a[13] * srcA + a[14] 
* Alphaͨ����ֵ= a[15] * srcR + a[16] * srcG +a[17] * srcB + a[18] * srcA + a[19] 
* ��ע��
* srcRΪԭͼRedͨ��ֵ��srcGΪԭͼGreenͨ��ֵ��srcBΪԭͼBlueͨ��ֵ��srcAΪԭͼAlphaͨ��ֵ��
* ÿ��ͨ����Դֵ����ֵ����0��255�ķ�Χ�ڡ���ʹ����������255��С��0��ֵ������������0��255�ķ�Χ�ڡ�
* 
* ��ť������
* 
*/
private final static float[] BUTTON_PRESSED = new float[] { 
1.39f, 0, 0, 0, -50, 
0, 1.39f, 0, 0, -50, 
0, 0, 1.39f, 0, -50, 
0, 0, 0, 5, 0 };

/**
* ��ť�ָ�ԭ״
*/
private final static float[] BUTTON_RELEASED = new float[] { 1, 0, 0, 0, 0,
	0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };

public MyImageButton(Context context) {
super(context);
this.setOnTouchListener(this);
this.setOnFocusChangeListener(this);
}

public MyImageButton(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.imageButtonStyle);
this.setOnTouchListener(this);
this.setOnFocusChangeListener(this);
}

public MyImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
this.setOnTouchListener(this);
this.setOnFocusChangeListener(this);
}


@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub

ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);

if (hasFocus) {
	((ImageButton) v).getDrawable().setColorFilter(
			new ColorMatrixColorFilter(BUTTON_PRESSED));
} else {
	((ImageButton) v).getDrawable().setColorFilter(
			new ColorMatrixColorFilter(BUTTON_RELEASED));
}

}

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);

if (event.getAction() == MotionEvent.ACTION_DOWN) {
	((ImageButton) v).getDrawable().setColorFilter(
			new ColorMatrixColorFilter(BUTTON_PRESSED));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
	((ImageButton) v).getDrawable().setColorFilter(
			new ColorMatrixColorFilter(BUTTON_RELEASED));

}

return false;

}

}
